/* 
 * imageGet.cpp
 * Created on: 2011-03-25
 * Author: MillionLee
 */
#include "StdAfx.h"
#include "imageGet.h"
#include "config.h"

#include <iostream>
#include <fstream>
#include <direct.h>

#include "..\TecentSDK\Source\http\Http.h"

imageGet::imageGet(void)
{
}

imageGet::~imageGet(void)
{
}

std::string imageGet::Get(const std::string &imageUrl, const std::string &postID)
{
	HTTP_RESULTCODE eResultCode = HTTPRESULT_FAIL;

	CHttp cHttp;
	std::string imageBinary;
	mkdir(STR_POSTS_DIR.c_str());//make posts dir

	std::string imageFilePath = STR_POSTS_DIR + postID;
	mkdir(imageFilePath.c_str());//make one post dir
 
	eResultCode = cHttp.SyncDownload(imageUrl + "/2000", imageBinary);//download fullsize image
	if(HTTPRESULT_OK != eResultCode){//download image failed
		rmdir(imageFilePath.c_str());
		return "";
	}

	std::string imageFilePathName = (STR_POSTS_DIR + postID + '\\' + postID + ".jpeg").c_str();
	std::ofstream imageFile(imageFilePathName.c_str(), std::fstream::out|std::fstream::trunc|std::fstream::binary);//create image file
	if(!imageFile){//create image file failed
		rmdir(imageFilePath.c_str());
		return "";
	}

	imageFile << imageBinary;//write

	imageFile.close();

	return imageFilePathName;
}

bool imageGet::confirmPath(const std::string &path)//useless
{
	int nPos = path.find('\\');

	while(-1 != nPos){
		
		if (mkdir("testdir"));

	}

	return true;
}